package com.grape.demo_graphql.resolver;

import com.grape.demo_graphql.entity.BankAccount;
import com.grape.demo_graphql.entity.Client;
import graphql.GraphqlErrorException;
import graphql.execution.DataFetcherResult;
import graphql.kickstart.execution.error.GenericGraphQLError;
import graphql.kickstart.tools.GraphQLResolver;
import graphql.parser.antlr.GraphqlVisitor;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author robin
 * @Date 2021/11/2
 * @Description
 */
@Slf4j
@Component
public class ClientResolver implements GraphQLResolver<BankAccount> {
//    public DataFetcherResult<Client> client(BankAccount bankAccount) {
//        log.info("client be create");
////        throw  new RuntimeException("client is null");
//        /*return Client.builder()
//                .id(UUID.randomUUID())
//                .firstName("robin1")
//                .lastName("pj1")
//                .build();*/
//        return DataFetcherResult.<Client>newResult()
//                .data(Client.builder()
//                        .id(UUID.randomUUID())
//                        .firstName("robin1")
//                        .lastName("pj1")
//                        .build())
//                .error(new GenericGraphQLError("can'nt get part of data"))
//                .localContext(new Date())
//                .build();
//    }
    private final ExecutorService service= Executors.newFixedThreadPool(
            Runtime.getRuntime().availableProcessors()
);
    public CompletableFuture<Client> client(BankAccount bankAccount){
        return CompletableFuture.supplyAsync(
                ()->{
                    log.info("create a client");
                  return   Client.builder()
                        .id(UUID.randomUUID())
                        .firstName("robin1")
                        .lastName("pj1")
                        .build();
                },
                service
        );
    }
}
